﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcBHXH.Common;

namespace MvcBHXH.Models
{
    public class MaDotBHModel
    {
        private DataClassesBHYTTNDataContext context;

        public MaDotBHModel()
        {
            context = new DataClassesBHYTTNDataContext();
        }

        public List<MaDotBHModel> SearchMaDotBHModel(string donVi, string tinh, string doiTuong, string loaiBH, string mucBH)
        {
            Boolean haslBH = false;
            Boolean hasTinh = false;
            Boolean hasDonVi = false;
            Boolean hasMucBH = false;

            if(loaiBH == "0")
            {
                haslBH = true;
            }

            if (tinh == "0")
            {
                hasTinh = true;
            }

            if (donVi == "0")
            {
                hasDonVi = true;
            }

            if (mucBH == null || mucBH == "0")
            {
                hasMucBH = true;
            }

            var dotBHModel = from fc in context.tbl_DotBHs
                             where (fc.Ma_DonViBH.Equals(donVi) || hasDonVi) && (fc.Ma_Tinh.Equals(tinh) || hasTinh) && fc.DoiTuong.Contains(doiTuong) && (fc.Ma_LoaiBH.Equals(loaiBH) || haslBH)
                             orderby fc.Ma_DotBH descending
                             select new
                             {
                                 fc.Ma_DotBH,
                                 fc.Ma_DonViBH,
                                 fc.Ma_Tinh,
                                 fc.Ma_Huyen,
                                 fc.DoiTuong,
                                 fc.NgayLap,
                                 fc.Ma_LoaiBH,
                                 fc.Ma_MucBH,
                                 fc.SoTien1Nam,
                                 fc.Ma_NSD,
                                 fc.NgayTaoMoi,
                                 fc.NgayCapNhat,
                             };
            List<MaDotBHModel> listMaDotBHModel = new List<MaDotBHModel>();
            MaDotBHModel dotBH;
            foreach (var item in dotBHModel)
            {
                dotBH = new MaDotBHModel();
                dotBH.MaDotBH = item.Ma_DotBH.ToString();
                dotBH.DonViBH = item.Ma_DonViBH;
                dotBH.MaTinh = item.Ma_Tinh;
                dotBH.Huyen = item.Ma_Huyen;
                dotBH.DoiTuong = item.DoiTuong;
                dotBH.NgayLap = (DateTime)item.NgayLap;
                dotBH.LoaiBH = item.Ma_LoaiBH;
                dotBH.MucBH = item.Ma_MucBH;
                dotBH.SoTien1Nam = (int)item.SoTien1Nam;
                dotBH.NguoiSudung = item.Ma_NSD;
                dotBH.NgayTaoMoi = item.NgayTaoMoi;
                //dotBH.NgayCapNhat = (DateTime)item.NgayCapNhat;
                listMaDotBHModel.Add(dotBH);

            }
            return listMaDotBHModel;
        }

        public List<MaDotBHModel> GetMaDotBHModel()
        {
            var dotBHModel = from fc in context.tbl_DotBHs
                             orderby fc.Ma_DotBH descending
                             select new
                             {
                                 fc.
                                 Ma_DotBH,
                                 fc.Ma_DonViBH,
                                 fc.Ma_Tinh,
                                 fc.Ma_Huyen,
                                 fc.DoiTuong,
                                 fc.NgayLap,
                                 fc.Ma_LoaiBH,
                                 fc.Ma_MucBH,
                                 fc.SoTien1Nam,
                                 fc.Ma_NSD,
                                 fc.NgayTaoMoi,
                                 fc.NgayCapNhat,
                             };
            List<MaDotBHModel> listMaDotBHModel = new List<MaDotBHModel>();
            MaDotBHModel dotBH;
            foreach (var item in dotBHModel)
            {
                dotBH = new MaDotBHModel();
                dotBH.MaDotBH = item.Ma_DotBH.ToString();
                dotBH.DonViBH = item.Ma_DonViBH;
                dotBH.MaTinh = item.Ma_Tinh;
                dotBH.Huyen = item.Ma_Huyen;
                dotBH.DoiTuong = item.DoiTuong;
                dotBH.NgayLap = (DateTime)item.NgayLap;
                dotBH.LoaiBH = item.Ma_LoaiBH;
                dotBH.MucBH = item.Ma_MucBH;
                dotBH.SoTien1Nam = (int)item.SoTien1Nam;
                dotBH.NguoiSudung = item.Ma_NSD;
                dotBH.NgayTaoMoi = item.NgayTaoMoi;
                //dotBH.NgayCapNhat = (DateTime)item.NgayCapNhat;
                listMaDotBHModel.Add(dotBH);

            }
            return listMaDotBHModel;
        }

        public MaDotBHModel GetDotBHById(string id)
        {
            var dotBHModel = from fc in context.tbl_DotBHs where fc.Ma_DotBH.Equals(id)
                             select new
                             {
                                 fc.Ma_DotBH,
                                 fc.Ma_DonViBH,
                                 fc.Ma_Tinh,
                                 fc.Ma_Huyen,
                                 fc.DoiTuong,
                                 fc.NgayLap,
                                 fc.Ma_LoaiBH,
                                 fc.Ma_MucBH,
                                 fc.SoTien1Nam,
                                 fc.Ma_NSD,
                                 fc.NgayTaoMoi,
                                 fc.NgayCapNhat,
                             };
            MaDotBHModel dotBH;
            foreach (var item in dotBHModel)
            {
                dotBH = new MaDotBHModel();
                dotBH.MaDotBH = item.Ma_DotBH.ToString();
                dotBH.DonViBH = item.Ma_DonViBH;
                dotBH.MaTinh = item.Ma_Tinh;
                dotBH.Huyen = item.Ma_Huyen;
                dotBH.DoiTuong = item.DoiTuong;
                dotBH.NgayLap = (DateTime)item.NgayLap;
                dotBH.LoaiBH = item.Ma_LoaiBH;
                dotBH.MucBH = item.Ma_MucBH;
                dotBH.SoTien1Nam = (int)item.SoTien1Nam;
                dotBH.NguoiSudung = item.Ma_NSD;
                dotBH.NgayTaoMoi = item.NgayTaoMoi;
                //dotBH.NgayCapNhat = (DateTime)item.NgayCapNhat;
                return dotBH;

            }
            return new MaDotBHModel();
        }

        public Boolean Insert(string DonViBH,string MaTinh,string Huyen,string DoiTuong,string NgayLap,
            string LoaiBH,string MucBH,string SoTien1Nam,string NguoiSudung)
        {
            context.usp_DotBH_InsertOne(DonViBH, MaTinh, Huyen, DoiTuong, DateTime.Parse(NgayLap), LoaiBH, MucBH, Int16.Parse(SoTien1Nam), NguoiSudung);
            context.SubmitChanges();
            return true;
        }

        public Boolean update(string MaDotBH, string DonViBH, string MaTinh, string Huyen, string DoiTuong, string NgayLap,
            string LoaiBH, string MucBH, string SoTien1Nam, string NguoiSudung)
        {
            Log.Write("Class" + this.GetType().Name + ".cs" + " Start Update() ");
            context.usp_DotBH_UpdateOne(Int16.Parse(MaDotBH), DonViBH, MaTinh, Huyen, DoiTuong, DateTime.Parse(NgayLap),
            LoaiBH, MucBH, Int16.Parse(SoTien1Nam), NguoiSudung);
            Log.Write("EXEC usp_DotBH_InsertOne '" + Int16.Parse(MaDotBH) + "', '" + DonViBH + "', '" + MaTinh + "', '" + Huyen + "', '" + DoiTuong + "', '" + DateTime.Parse(NgayLap) + "', '" + LoaiBH + "', '" + MucBH + "', '" + Int16.Parse(SoTien1Nam) + "', '" + NguoiSudung + "'");
            context.SubmitChanges();
            Log.Write("Class" + this.GetType().Name + " End Update() ");
            return true;
        }

        public Boolean Delete(string id)
        {
            context.usp_DotBH_DeleteOne(Int16.Parse(id));
            context.SubmitChanges();
            return true;
        }


        private string maDotBH;

        public string MaDotBH
        {
            get { return maDotBH; }
            set { maDotBH = value; }
        }
        private string donViBH;

        public string DonViBH
        {
            get { return donViBH; }
            set { donViBH = value; }
        }
        private string maTinh;

        public string MaTinh
        {
            get { return maTinh; }
            set { maTinh = value; }
        }
        private string huyen;

        public string Huyen
        {
            get { return huyen; }
            set { huyen = value; }
        }
        private string doiTuong;

        public string DoiTuong
        {
            get { return doiTuong; }
            set { doiTuong = value; }
        }
        private DateTime ngayLap;

        public DateTime NgayLap
        {
            get { return ngayLap; }
            set { ngayLap = value; }
        }
        private string loaiBH;

        public string LoaiBH
        {
            get { return loaiBH; }
            set { loaiBH = value; }
        }
        private string mucBH;

        public string MucBH
        {
            get { return mucBH; }
            set { mucBH = value; }
        }
        private int soTien1Nam;

        public int SoTien1Nam
        {
            get { return soTien1Nam; }
            set { soTien1Nam = value; }
        }
        private string nguoiSudung;

        public string NguoiSudung
        {
            get { return nguoiSudung; }
            set { nguoiSudung = value; }
        }
        private DateTime ngayTaoMoi;

        public DateTime NgayTaoMoi
        {
            get { return ngayTaoMoi; }
            set { ngayTaoMoi = value; }
        }
        private DateTime ngayCapNhat;

        public DateTime NgayCapNhat
        {
            get { return ngayCapNhat; }
            set { ngayCapNhat = value; }
        }

    }
}